<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/29/029
 * Time: 9:31
 */

namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\api\model\Category as CategoryModel;
use app\api\validate\CategoryValidate;
use app\lib\exception\CategoryException;

class Category extends BaseController {

    /**
     * 获取所有正常的分类
     * @url   category_list
     * @method   get
     * @return array|\PDOStatement|string|\think\Collection
     */
    public function get_category() {
        $category = CategoryModel::get_category_list();
        return $category;
    }

    /**
     * 根据KEY获取分类
     * @param $key
     * @return array|\PDOStatement|string|\think\Collection
     * @throws CategoryException
     */
    public function getCategoryByKey($key) {
        (new CategoryValidate())->goCheck();

        $cate = CategoryModel::getCategoryByKey($key);

        if (!$cate) {
            throw new CategoryException();
        }

        return $cate;
    }
}